// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  //存储数据库
  await db.collection("msg").add({
    data:{
      MsgType: event.MsgType,
      CreateTime: event.CreateTime,
      Content: event.Content,
      FromUserName: event.FromUserName,
      ToUserName: event.ToUserName,
      MsgId: event.MsgId,
      Status: 0
    },
  })
  if(wxContext.OPENID!="oZk6u5Vw4Q6OvMQgqDe-pRIYGTac"){
    await cloud.openapi.customerServiceMessage.send({
      touser:"oZk6u5Vw4Q6OvMQgqDe-pRIYGTac",
      msgtype: 'text',
      text: {
        content: event.Content,
      },
    })
  }
  if(wxContext.OPENID!="oZk6u5Vw4Q6OvMQgqDe-pRIYGTac"){
    await cloud.openapi.customerServiceMessage.send({
      touser: wxContext.OPENID,
      msgtype: 'text',
      text: {
        content: '收到',
      },
    })
  }
  return "sucess"
}